package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.HealthInstitution;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.HealthInstitutionService;
import com.wty.cmims.mapper.HealthInstitutionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【health_institution】的数据库操作Service实现
* @createDate 2024-11-11 21:58:33
*/
@Service
public class HealthInstitutionServiceImpl extends ServiceImpl<HealthInstitutionMapper, HealthInstitution>
    implements HealthInstitutionService{
    @Autowired
    private HealthInstitutionMapper healthInstitutionMapper;

    @Override
    public ResponseResult<Map<String, Object>> healthInstitutionFind(Map<String,Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        if (paramsMap.get("pageSize") != null || paramsMap.get("currentPage") != null){
            Integer pageSize = (Integer) paramsMap.get("pageSize");
            paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
            responseResult.setPageSize(pageSize);
        }
        List<Map<String, Object>> healthInstances = healthInstitutionMapper.findHealthInstitutions(paramsMap);
        Integer total = healthInstitutionMapper.findHealthInstitutionsNum(paramsMap);

        if (healthInstances != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询医疗机构信息成功");
            responseResult.setDatas(healthInstances);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询医疗机构信息失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> healthInstitutionUpdate(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        Integer rows = healthInstitutionMapper.updateHealthInstitution(paramsMap);
        if (rows > 0) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("医疗机构信息更新成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("医疗机构信息更新失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> healthInstitutionAdd(Map<String, Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        HealthInstitution healthInstitution = new HealthInstitution();
        healthInstitution.setName((String) paramsMap.get("name"));
        healthInstitution.setTypeId((Integer) paramsMap.get("institutionTypeId"));
        healthInstitution.setLevel(Integer.parseInt((String) paramsMap.get("level")));
        healthInstitution.setAddress((String) paramsMap.get("address"));
        healthInstitution.setDescription((String) paramsMap.get("description"));
        healthInstitution.setContact((String) paramsMap.get("contact"));
        healthInstitution.setInstitutionImg((String) paramsMap.get("institution_img"));
        healthInstitution.setIsDel(0);
        boolean save = this.save(healthInstitution);
        if (save) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("医疗机构添加成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("医疗机构添加失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> healthInstitutionDelete(Integer institutionId) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<HealthInstitution> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(HealthInstitution::getId,institutionId);
        wrapper.set(HealthInstitution::getIsDel,1);
        boolean update = this.update(wrapper);
        if (update) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("医疗机构删除成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("医疗机构删除失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> healthInstitutionDropDown(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Map<String, Object>> healthInstances = healthInstitutionMapper.findHealthInstitutions(paramsMap);
        if (healthInstances != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询医疗机构信息成功");
            responseResult.setDatas(healthInstances);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询医疗机构信息失败");
        }
        return responseResult;
    }
}